package cn.edu.hunau.service.impl;


import cn.edu.hunau.dao.AddressDao;
import cn.edu.hunau.dao.OrderDao;
import cn.edu.hunau.dao.OrderItemDao;
import cn.edu.hunau.dao.impl.AddressDaoImpl;
import cn.edu.hunau.dao.impl.OrderDaoImpl;
import cn.edu.hunau.dao.impl.OrderItemDaoImpl;
import cn.edu.hunau.exception.DaoException;
import cn.edu.hunau.pojo.*;
import cn.edu.hunau.service.OrderService;
import cn.edu.hunau.utils.DBUtils;
import cn.edu.hunau.utils.UUIDUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 苏佳
 * @Date 2022 11 13 20 59
 **/
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private OrderItemDao orderItemDao= new OrderItemDaoImpl();

    private AddressDao addressDao=new AddressDaoImpl();

    @Override
    public Order createOrder(User user, Car car) {
        //从car中提取出order信息
        Order o = new Order();
        o.setAccount(car.getTotal());
        //获取订单编号
        o.setId(UUIDUtils.uuid());
        o.setUser(user);


        //从car中获取orderItem的信息
        List<CarItem> carItems = car.getItems();
        ArrayList<OrderItem> orderItems = new ArrayList<>();
        carItems.forEach(carItem -> {
            OrderItem item = new OrderItem();
            item.setProduct(carItem.getProduct());
            item.setQty(carItem.getQty());
            item.setSize(carItem.getSize());
            item.setShoppingPrice(carItem.getShoppingPrice());
            item.setOrder(o);
            orderItems.add(item);
        });

        //必须是在业务层进行事务管理
        Connection conn = null;
        try {
            conn= DBUtils.beginTransaction();

            orderDao.insert(o);
            orderItemDao.insert(orderItems);
            //手动提交事务
            DBUtils.commit(conn);
            o.setItems(orderItems);
            return o;
        } catch (DaoException e) {
            DBUtils.rollback(conn);
            throw new DaoException(e);
        } finally {
            DBUtils.close(conn);
        }

    }

    @Override
    public void payOrder(String oid, Address address) {
        Connection conn = null;
        try {
            conn=DBUtils.beginTransaction();
            //向t_address中插入数据
            int aid = addressDao.insert(address);
            //根据oid更新aid
            orderDao.updateAid(oid,aid);
            //更新订单状态
            orderDao.updateOstate(oid);

            //手动提交事务
            DBUtils.commit(conn);
        } catch (DaoException e) {
            DBUtils.rollback(conn);
            throw new DaoException(e);
        } finally {
            DBUtils.commit(conn);
        }
    }

    @Override
    public List<Order> loadOrder(int uid) {
        List<Order> orders = orderDao.queryByUid(uid);
        List<String> list = orders.stream()
                .map(order -> order.getId())
                .collect(Collectors.toList());
        for (int i = 0; i < orders.size(); i++) {
            List<OrderItem> orderItems = orderItemDao.queryByOid(list.get(i));
            Address address = addressDao.queryByAid(orders.get(i).getAddress().getId());
            orders.get(i).setItems(orderItems);
            orders.get(i).setAddress(address);
        }
        return orders;
    }

    @Override
    public void removeOrder(String oid) {
        Connection conn = null;
        try {
            conn=DBUtils.beginTransaction();
            orderDao.deleteOrderByOid(oid);
            orderItemDao.deleteOrderItemBy(oid);
            //手动提交事务
            DBUtils.commit(conn);
        } catch (DaoException e) {
            DBUtils.rollback(conn);
            throw new DaoException(e);
        } finally {
            DBUtils.commit(conn);
        }

    }

    @Override
    public int countByOstate(int uid,String ostate) {
        return orderDao.countByOstate(uid,ostate);
    }
}
